package app.server.s01;

import java.util.HashMap;
import java.util.Map;

import lasun.ConfStart;
import pub.itf.IPubParam;
import system.model.SmParam;
import app.model.AppBasedoc;
import app.pub.IAppFilePath;

import com.agile.jfinal.controller.CoreController;
import com.jfinal.ext.render.DwzRender;
import com.jfinal.upload.UploadFile;

/**
 * APP基本管理档案
 * @author qiutian  2014年03月24日
 */
public class AppBasedocController extends CoreController {

	
	/**
	 * 默认方法查询数据
	 */
	public void index(){
		
		AppBasedoc appBasedoc = getModel(AppBasedoc.class);
		
		appBasedoc.setLikeColumn("vcode,vname");
		setAttr("appBasedoc", appBasedoc);
		
		setAttr("page",appBasedoc.queryPaginate(getPageNumber(), getPageSize())); //分页查询
		renderJsp("/WEB-INF/jsp/app/server/s01/app_basedoc_list.jsp");
	}

	/**
	 * 保存数据
	 */
	public void saveModel(){
		
		try {
			UploadFile iconFile = getFile("icon");
			UploadFile picWelcomeFile = getFile("pic_welcome");
			
			AppBasedoc appBasedoc = getModel(AppBasedoc.class);
			
			//在指定位置重新生成文件
//			File _iconfile = new File(getRequest().getRealPath("")+"/"+IUploadPath.icon_path+"/"+appBasedoc.getStr(appBasedoc.VCODE)+"_"+System.currentTimeMillis()+".png");
//			iconFile.getFile().renameTo(_iconfile);
			//设置文件访问路径
			if(iconFile!=null ) appBasedoc.set(appBasedoc.ICON, SmParam.getParamValue("server.url")+"upload/"+iconFile.getFileName());
			if(picWelcomeFile!=null )appBasedoc.set(appBasedoc.PIC_WELCOME, SmParam.getParamValue("server.url")+"upload/"+picWelcomeFile.getFileName());
			
			appBasedoc.set(appBasedoc.IS_UPDATE, 1);
		
			if(appBasedoc.save()){
				
				DwzRender dwz = new DwzRender();
				dwz.message("操作成功");
				dwz.callbackType("closeCurrent");
				dwz.navTabId("app_basedoc_list");
				render(dwz);
			}else{
				render(DwzRender.error());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logError("保存信息失败");
			render(DwzRender.error());
		}
		
	}
	
	/**
	 * 一键生成App
	 */
	public void createApp(){
		try {
			AppBasedoc appBasedoc = getModel(AppBasedoc.class);
			appBasedoc = appBasedoc.queryById();
			
			String copyUrl = getRequest().getRealPath("")+"/"+IAppFilePath.path_apk;
			/* map.put(“pic_welcome","欢迎界面链接");
			 * map.put(“app_url","主页面链接"); map.put(“app_menu","自定义菜单");
			 * map.put(“appCode","app编码"); map.put(“serverIP","自定义菜单");*/
			
			//传递数据的Map
			Map<String, Object> appMap = new HashMap<String,Object>();
			
			appMap.put("app_url",appBasedoc.getStr(appBasedoc.APP_URL)); 
			appMap.put("appCode",appBasedoc.getStr(appBasedoc.VCODE)); 
			appMap.put("app_name",appBasedoc.getStr(appBasedoc.VNAME)); 
			appMap.put("serverIP",SmParam.getParamValue("server.ip"));
			appMap.put("pic_welcome",appBasedoc.getStr(appBasedoc.PIC_WELCOME)); 
			appMap.put("icon",appBasedoc.getStr(appBasedoc.ICON)); 
			
			if(ConfStart.creatConfig(appMap, copyUrl)){
				//保存下载地址
				String download_url ="/"+IAppFilePath.path_apk+"/"+appBasedoc.getStr(appBasedoc.VCODE)+".apk";
				appBasedoc.set(appBasedoc.DOWNLOAD_URL, download_url);
				appBasedoc.save();
				
				DwzRender dwz = new DwzRender();
				dwz.message("操作成功");
				dwz.navTabId("app_basedoc_list");
				render(dwz);
			}else{
				logError("生成App失败，请检查配置！");
				render(DwzRender.error("生成App失败，请检查配置！"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			render(DwzRender.error());
		}
	}
	
	
	/**
	 * 下载功能
	 */
	public void downloadApp(){
		
		AppBasedoc appBasedoc = getModel(AppBasedoc.class);
		appBasedoc = appBasedoc.queryById();
		renderFile(appBasedoc.getStr(appBasedoc.DOWNLOAD_URL));
//		renderJsp("http://localhost:8080/lasunApp/download/app/20140331001.apk");
	}
	
	
	
	/**
	 * 转入新增页面
	 */
	public void addPage(){
		renderJsp("/WEB-INF/jsp/app/server/s01/app_basedoc_form.jsp");
	}
	
	/**
	 * 转入修改页面
	 */
	public void editPage(){
		setAttr("appBasedoc", getModel(AppBasedoc.class).queryById());
		renderJsp("/WEB-INF/jsp/app/server/s01/app_basedoc_form.jsp");
	}
	
	/**
	 * 删除数据
	 */
	public void delModel(){
		getModel(AppBasedoc.class).delete();
		render(DwzRender.success());
	}
}
